<template>
    <div>
        <h2>你好</h2>
        <div class="input-div one" id="emailBox">
            <div class="i">
                <i class="fas fa-user"></i>
            </div>
            <div>
                <h5>{{ selectLoginTypeToInfos[$store.state.loginModule.selectLoginType] }}电子邮箱</h5>
                <input class="input" type="text" ref="email">
            </div>
        </div>
        <input class="btn" type="submit" value="发送验证码" @click="sendVerificationCode($event)">
    </div>
</template>

<script>
export default {
    name: "SendVerificationCode",

    data() {
        return {
            // 本页面的计时器
            interval: null,

            selectLoginTypeToInfos: {
                "loginByGuardianEmail": "监护人",
                "loginBySelfEmail": "本人"
            }
        }
    },

    methods: {
        init() {
            this.$(() => {
                const inputs = document.querySelectorAll('.input');

                function focusFunc() {
                    let parent = this.parentNode.parentNode;
                    parent.classList.add('focus');
                }

                function blurFunc() {
                    let parent = this.parentNode.parentNode;
                    if (!this.value) {
                        parent.classList.remove('focus');
                    }
                }

                inputs.forEach(input => {
                    input.addEventListener('focus', focusFunc);
                    input.addEventListener('blur', blurFunc);
                });
            })

        },

        sendVerificationCode($event) {
            let email = this.$refs.email.value

            if (!email) return this.$message({
                message: '电子邮箱不能为空',
                type: 'warning'
            })

            // 验证填写邮箱是否规范
            let reg = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/
            if (!reg.test(email)) return this.$message({
                message: '请输入正确的电子邮箱',
                type: 'warning'
            })

            // 按钮禁止点击
            this.$public.bottomDisableOrAllow($event.target, "正在发送验证码", true)

            // 发送验证码
            this.request.post("/user/sendVerificationCode/", {
                email: email
            }).then((response) => {
                const status = response["code"] === 200

                if (status) {
                    // 进入60s倒计时，然后才可进行重发
                    let second = 60
                    this.interval = setInterval(() => {
                        if (!second) {
                            clearInterval(this.interval)
                            this.$public.bottomDisableOrAllow($event.target, "重新发送验证码", false)
                        } else {
                            this.$public.bottomDisableOrAllow($event.target, `重新发送验证码(${second}s)`, true)
                            second--
                        }
                    }, 1000)

                    // 将emailVerificationRecordId记录在公共空间的uid内，后续验证登录需要用到
                    this.$store.state.loginModule.emailVerificationRecordId = response["data"]["emailVerificationRecordId"]

                    // 渲染输入验证码组件
                    this.$router.push("/loginView/checkVerificationCode/")

                } else {
                    this.$public.bottomDisableOrAllow($event.target, "重新发送验证码", false)
                }
            }).catch((error) => {
                this.$public.bottomDisableOrAllow($event.target, "重新发送验证码", false)
            })
        },
    },

    created() {
        this.init()
    },

    beforeDestroy() {
        if (this.interval) {
            clearInterval(this.interval)
        }
    }
}
</script>

<style lang="less" scoped>
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.input-div {
    position: relative;
    display: grid;
    grid-template-columns: 7% 93%;
    margin: 25px 0;
    padding: 5px;
    border-bottom: 2px solid #037971;
}

.input-div:after, .input-div:before {
    content: '';
    position: absolute;
    bottom: -2px;
    width: 0;
    height: 2px;
    background-color: #38d39f;
    transition: .3s;
}

.input-div:after {
    right: 50%;
}

.input-div:before {
    left: 50%;
}

.input-div.one {
    margin-top: 0;
}

.i {
    display: flex;
    justify-content: center;
    align-items: center;
}

.i i {
    color: #037971;
    transition: .3s;
}

.input-div > div {
    position: relative;
    height: 45px;
}

.input-div > div h5 {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #2b817b;
    font-size: 18px;
    transition: .3s;
}

.input {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    border: none;
    outline: none;
    background: none;
    padding: 0.5rem 0.7rem;
    font-size: 1.2rem;
    font-family: 'Poppins', sans-serif;
    color: #555;
}

a {
    display: block;
    text-align: right;
    text-decoration: none;
    color: #999;
    font-size: 0.9rem;
    transition: .3s;
}

a:hover {
    color: #037971;
}

.btn {
    display: block;
    width: 100%;
    height: 50px;
    border-radius: 25px;
    margin: 1rem 0;
    font-size: 1.2rem;
    outline: none;
    border: none;
    background-image: linear-gradient(to right, #32be8f, #38d39f, #32be8f);
    cursor: pointer;
    color: #fff;
    text-transform: uppercase;
    font-family: 'Poppins', sans-serif;
    background-size: 200%;
    transition: .5s;
}

.input-div.focus .i i {
    color: #38d39f;
}

.input-div.focus div h5 {
    top: -5px;
    font-size: 15px;
    color: #38d39f;
}

.btn:hover {
    background-position: right;
}

h2 {
    font-size: 2.9rem;
    text-transform: uppercase;
    margin: 8px 0 15px;
    color: #037971;
}

@media screen and (max-width: 1000px) {
    h2 {
        font-size: 2.4rem;
        margin: 8px 0 15px;
    }
}
</style>
